<?php
class Project_Users_m extends MY_Model{
	
	function __construct(){
		parent::__construct();
		$this->_table = 'business_project_users';
	}
	
	public function insert($id,$user_array = array(),$not_important = TRUE){
		$data = array('id'=>$id,'user_id'=>0);
		$j = 0;
		foreach($user_array as $i){
			$j++;
			$data['user_id']=$i;
			$data['important'] = 0;
			if(!empty($not_important)){
				if($j==1)
					$data['important'] = 1;
			}
			parent::insert($data);
			
		}
	}
	
	public function get_all($param = array()){
		
		if(!empty($param['id'])){
			$this->_table = 'business_project_users p';
			$this->db->select('u.display_name,u.username,p.user_id,p.important,p.read');
			$this->db->join('ser_users u','u.id = p.user_id');	
			$this->db->order_by('p.important','desc');
			$this->db->where('p.id',$param['id']);
		}
		return parent::get_all();
	}
	
	public function update($id,$id2,$data){
		$this->_table = 'business_project_users';
		$this->db->where('id',$id);
		parent::update($id,array('important'=>0));
		
		$this->db->where('id',$id);
		$this->db->where('user_id',$id2);
		$this->db->update($this->_table,$data);
		
	}
	//xoa list nhan vien khoi cong viec
	public function delete($id,$data){
		foreach($data as $item){
			$this->db->where('id',$id);
			$this->db->where('user_id',$item);
			$this->db->delete($this->_table);
		}
	}
	
	public function delete_by_key($id){
		parent::delete($id);
	}
	public function update_read($pid,$user_id){
		//get
		$this->db->where('id',$pid);
		$this->db->where('user_id',$user_id);
		$single = $this->db->get($this->_table)->row();
		if(count($single)>0){
			$update = array('read'=>$single->read + 1);
			$this->db->where('id',$pid);
			$this->db->where('user_id',$user_id);
			$this->db->update($this->_table,$update);
		}
		//echo $this->db->last_query();exit;
	}
}